package com.drdj.exam;


import java.util.Stack;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;

public class FlipViews extends Activity {

	
	private GestureDetector mDetector;
	private ViewFlipper mVfFlipper;
	
	Animation leftIn;
	Animation rightIn;
	Animation leftOut;
	Animation rightOut;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.flip_views);

		mVfFlipper = (ViewFlipper) findViewById(R.id.vfFlipper);
		
		mDetector = new GestureDetector(this, new GestureListener());
		
		int width = 480;
		
//		leftIn = new TranslateAnimation(-width, 0, 0, 0);
//		rightOut = new TranslateAnimation(0, width, 0, 0);
		leftIn = AnimationUtils.loadAnimation(FlipViews.this, android.R.anim.slide_in_left);
		rightOut = AnimationUtils.loadAnimation(FlipViews.this, android.R.anim.slide_out_right);

		rightIn = new TranslateAnimation(width, 0, 0, 0);
		leftOut = new TranslateAnimation(0, -width, 0, 0);
		
		leftIn.setDuration(1000);
		rightIn.setDuration(1000);
		leftOut.setDuration(1000);
		rightOut.setDuration(1000);
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (mDetector.onTouchEvent(ev)) {
			return true;
		}
		return super.dispatchTouchEvent(ev);
	}
	
	class GestureListener implements OnGestureListener{

		@Override
		public boolean onDown(MotionEvent e) {
			return false;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			
			if(Math.abs(velocityX) < 200){
				return false;
			}else{
				if(velocityX > 0){
					mVfFlipper.setInAnimation(leftIn);
					mVfFlipper.setOutAnimation(rightOut);
					mVfFlipper.showPrevious();
				}else if(velocityX < 0){
					mVfFlipper.setInAnimation(rightIn);
					mVfFlipper.setOutAnimation(leftOut);
					mVfFlipper.showNext();
				}
			}
			return true;
		}

		@Override
		public void onLongPress(MotionEvent e) {
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			return false;
		}

		@Override
		public void onShowPress(MotionEvent e) {
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			return false;
		}
		
	}
	
}
